package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Weeklynews;
import com.zzxz.practicaltrainingsys.mapper.WeeklynewsMapper;
import com.zzxz.practicaltrainingsys.service.WeeklynewsService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Mapper
public class WeeklynewsServiceImpl implements WeeklynewsService {
    @Autowired
    private WeeklynewsMapper weeklynewsMapper;



    //用来联合学生表的学号以及项目表的项目编号来查该项目该学生的周报
    @Override
    public List<Weeklynews> getWeeklynewsByStudentId(Integer studentId) {
        if (studentId == null) {
            throw new IllegalArgumentException("学生ID不能为空");
        }
        return weeklynewsMapper.getWeeklynewsByStudentId(studentId);
    }

    @Override
    public Weeklynews getWeeklynewsById(Integer weeklynewsId) {
        if (weeklynewsId == null) {
            throw new IllegalArgumentException("周报 ID 不能为空");
        }
        return weeklynewsMapper.getWeeklynewsById(weeklynewsId);
    }

    @Override
    public int insertWeeklynews(Weeklynews weeklynews) {
        return weeklynewsMapper.insertWeeklynews(weeklynews);
    }

    @Override
    public int updateWeeklynews(Weeklynews weeklynews) {
        return weeklynewsMapper.updateWeeklynews(weeklynews);
    }

    @Override
    public List<Map<String, Object>> getAllWeeklyNews() {
        return weeklynewsMapper.getAllWeeklyNews();
    }

    @Override
    public PageInfo<Map<String, Object>> getWeeklyNewsByPage(Integer pageNum, Integer pageSize) {
        // 1. 参数校验（避免 null 或非法值）
        if (pageNum == null || pageNum <= 0) pageNum = 1;
        if (pageSize == null || pageSize <= 0) pageSize = 10;

        // 2. 启动 PageHelper 分页（必须紧跟查询）
        PageHelper.startPage(pageNum, pageSize);

        // 3. 执行查询（PageHelper 会自动拦截并添加 LIMIT 语法）
        List<Map<String, Object>> weeklyNewsList = weeklynewsMapper.getAllWeeklyNews();

        // 4. 用 PageInfo 包装结果（包含总记录数、分页信息）
        return new PageInfo<>(weeklyNewsList);
    }

    @Override
    public List<Weeklynews> getLeaderWeeklyByGroup(Integer projectId, Integer groupId) {
        return weeklynewsMapper.getLeaderWeeklyByGroup(projectId, groupId);
    }


    @Override
    public List<Map<String, Object>> getWeeklyNewsByProjectId(Integer projectId) {
        if (projectId == null) {
            throw new IllegalArgumentException("项目ID不能为空");
        }
        return weeklynewsMapper.getWeeklyNewsByProjectId(projectId);
    }
}
